package com.tongtech.space.common.utils;

import lombok.Data;
import java.security.MessageDigest;

@Data
public class String2MD5Util {
	/**
	 * 对字符串进行MD5加密
	 * @param str
	 * @return
	 */
	public static String string2MD5(String str) {
		MessageDigest md5 = null;
		try {
			md5 = MessageDigest.getInstance("MD5");
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
		char[] charArr = str.toCharArray();
		byte[] byteArr = new byte[charArr.length];
		
		for (int i = 0; i < charArr.length; i++) {
			byteArr[i] = (byte) charArr[i];
		}
		
		byte[] md5Bytes = md5.digest(byteArr);
		StringBuffer hexValue = new StringBuffer();
		for (int i = 0; i < md5Bytes.length; i++) {
			int val = md5Bytes[i] & 0xff;
			if (val < 16) {
				hexValue.append("0");
			}
			hexValue.append(Integer.toHexString(val));
		}
		return hexValue.toString();
	}

	public static void main(String[] args) {
		String appId = "7354817378853753";
		String appKey = "smNRjGTdX1d5DJh696";
		long time = System.currentTimeMillis();
		String signKey = appId + appKey + time;
		String sign = string2MD5(signKey);

		System.out.println("时间戳:" + time);
		System.out.println(signKey);
		System.out.println(sign);
	}

}
